
import { numberKeyboardProps } from "vant";
import { EnumBetResult, EnumBjlResult } from "../../enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IRecmStrategy } from "../IRecmStrategy";

export interface IFollowOption {
    followCount: number;
    reverse: boolean;
}
/**
 * 跟投策略
 */
export class FollowStrategy implements IRecmStrategy {
    /**
     * 配置
     */
    option: IFollowOption;
    strategyName: string = "FollowStrategy";
    debugCompoenent: string = "FollowStrategyDebug";
    /**
     * 构造函数
     */
    constructor(option?: IFollowOption) {
        if (option) {
            this.option = option;
        }
        else {
            this.option = { followCount: 1, reverse: false };
        }

    }
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as FollowStrategy;
        if (obj) {
            this.strategyName = obj.strategyName;
            this.debugCompoenent = obj.debugCompoenent;
            Object.assign(this.option, obj.option);
        }
    }
    setResult(roundInfo: RoundInfo, recmInfo: RecmInfo, multiple: number, waterRate: number, fuliRate: number): void {
        // let recmMoney = recmInfo.recmMoney;
        // if (multiple > 0) {
        //     recmMoney = recmMoney * multiple;
        // }
        //recmInfo.summaryInfo.updateSummaryInfo(roundInfo.currentResult, recmInfo.recmV, recmMoney, waterRate, fuliRate)
    }
    /**
     * 设置参数配置
     * @param option 参数配置
     */
    setOption<T>(option: T): void {
        this.option = option as IFollowOption;
        if (!this.option) throw new Error("【跟投策略】参数不正确");
    }
    toResult(roundInfo: RoundInfo): EnumBjlResult {
        let count = roundInfo.results.length;
        let result = EnumBjlResult.U;
        if (count >= this.option.followCount) {
            result = roundInfo.results[count - this.option.followCount];
        }
        return result;
    }

}


/**
 * 跟投策略
 */
export class FollowResultStrategy implements IRecmStrategy {
    /**
     * 配置
     */
    option: IFollowOption;
    strategyName: string = "FollowStrategy";
    debugCompoenent: string = "FollowStrategyDebug";
    /**
     * 构造函数
     */
    constructor(option?: IFollowOption) {
        if (option) {
            this.option = option;
        }
        else {
            this.option = { followCount: 1, reverse: false };
        }

    }
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as FollowStrategy;
        if (obj) {
            this.strategyName = obj.strategyName;
            this.debugCompoenent = obj.debugCompoenent;
            Object.assign(this.option, obj.option);
        }
    }
    setResult(roundInfo: RoundInfo, recmInfo: RecmInfo, multiple: number, waterRate: number, fuliRate: number): void {
    }

    toResult(roundInfo: RoundInfo): EnumBjlResult {
        let recmV = roundInfo.userRecm;
        let count = roundInfo.betResultArray.length;
        let result = EnumBetResult.NotBet;
        let reverse = false;
        if (count >= this.option.followCount) {
            result = roundInfo.betResultArray[count - this.option.followCount];
            if (result == EnumBetResult.Fail) {
                reverse = true;
            }
            else if (result == EnumBetResult.Success) {
                reverse = false;
            }
            else {
                recmV = EnumBjlResult.U;
            }
        }
        else {
            recmV = EnumBjlResult.U;
        }

        if (reverse && recmV != EnumBjlResult.U) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }


        if (this.option.reverse && recmV != EnumBjlResult.U) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }
        return recmV;
    }

}

export interface FollowArrayOption {
    followCount: number;
    reverse: boolean;
}
export class FollowArrayStrategy implements IRecmStrategy {
    /**
     *
     */
    protected _followCount: number = 1;
    protected _reverse: boolean = false;
    protected _arecm: EnumBjlResult = EnumBjlResult.U;
    protected _aresult: EnumBetResult = EnumBetResult.NotBet;
    public get reverse() {
        return this._reverse;
    };
    public get followCount() {
        return this._followCount;
    }
    public get ARecm() {
        return this._arecm;
    }
    public get AResult() {
        return this._aresult;
    }
    constructor(name: string, option?: FollowArrayOption) {
        this.strategyName = name;
        if (option) {
            this._followCount = option.followCount;
            this._reverse = option.reverse;
        }
        else {
            this._followCount = 1000;
            this._reverse = false;
        }

    }
    setResult(roundInfo: RoundInfo, recmInfo: RecmInfo, multiple: number, waterRate: number, fuliRate: number): void {
        let recmMoney = recmInfo.recmMoney;
        if (multiple > 0) {
            recmMoney = recmMoney * multiple;
        }

        if (this._arecm != EnumBjlResult.U) {
            this._aresult = this._arecm == roundInfo.currentResult ? EnumBetResult.Success : EnumBetResult.Fail;
        }

        recmInfo.summaryInfo.updateSummaryInfo(roundInfo.currentResult, recmInfo.recmV, recmMoney, waterRate, fuliRate)
    }
    toResult(roundInfo: RoundInfo): EnumBjlResult {
        let iv = 0;
        let recmV = EnumBjlResult.U;
        for (let i = 0; i < roundInfo.results.length && i < this.followCount; i++) {
            let rv = roundInfo.results[roundInfo.results.length - (i + 1)]
            if (rv == EnumBjlResult.Z) {
                iv++;
            }
            else if (rv == EnumBjlResult.X) {
                iv--;
            }
            else { }
        }
        if (iv > 0) {
            recmV = EnumBjlResult.Z;
        }
        else if (iv < 0) {
            recmV = EnumBjlResult.X;
        }

        this._arecm = recmV;
        if (recmV != EnumBjlResult.U && this.reverse) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }
        return recmV;
    }
    strategyName: string = "FollowArrayStrategy";
    debugCompoenent: string = "";
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as FollowArrayStrategy;
        if (obj) {
            this._arecm = obj._arecm;
            this._aresult = obj._aresult;
        }
    }

}

export class FollowJumpStrategy extends FollowArrayStrategy {
    strategyName: string = "";
    debugCompoenent: string = "";
    constructor(name: string, option?: FollowArrayOption) {
        super(name, option);
    }
    toResult(roundInfo: RoundInfo): EnumBjlResult {
        super.toResult(roundInfo);
        let recmV = this._arecm;
        if (this._arecm != EnumBjlResult.U && this._aresult == EnumBetResult.Fail) {
            recmV = this._arecm == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }
        return recmV;
    }

}

/**
 * 根据跟投N的第M个结果决定是否反向投注
 */
export class FollowFixedResultStrategy implements IRecmStrategy {
    /**
     * 配置
     */
    protected _followCount: number = 1;
    protected _luckyWay: number = 1;
    strategyName: string = "FollowStrategy";
    debugCompoenent: string = "FollowStrategyDebug";

    protected _result: EnumBetResult = EnumBetResult.NotBet;

    public get result() {
        return this._result;
    }
    /**
     * 构造函数
     */
    constructor(followCount?: number, luckyWay?: number) {
        this._followCount = followCount ?? 1;
        this._luckyWay = luckyWay ?? 1;

    }
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        var obj = valueObj as FollowFixedResultStrategy;
        if (obj) {
            this._followCount = obj._followCount;
            this._luckyWay = obj._luckyWay;
            this.strategyName = obj.strategyName;
            this.debugCompoenent = obj.debugCompoenent;
            this._result = obj._result;
        }
    }
    setResult(roundInfo: RoundInfo, recmInfo: RecmInfo, multiple: number, waterRate: number, fuliRate: number): void {

    }
    toResult(roundInfo: RoundInfo): EnumBjlResult {
        let count = roundInfo.results.length;
        let recmV = EnumBjlResult.U;
        if (count >= (this._followCount + this._luckyWay)) {
            //if (count > this._followCount && count > this._luckyWay) {
            let followRecmV = roundInfo.results[count - this._followCount];
            recmV = followRecmV;

            let yunshiRecmV = roundInfo.results[count - this._followCount - this._luckyWay]; //roundInfo.results[count - this._followCount - this._luckyWay + 2];
            //let yunshiResult = roundInfo.results[count - this._followCount - this._luckyWay + 1];
            let yunshiResult = roundInfo.results[count - this._luckyWay];
            // let yunshiRecmV = roundInfo.results[count - this._followCount - 1];
            // let yunshiResult = roundInfo.results[count - this._luckyWay]

            if (yunshiRecmV != EnumBjlResult.U && yunshiResult != EnumBjlResult.U) {
                if (yunshiRecmV == yunshiResult) {
                    this._result = EnumBetResult.Success;
                }
                else {
                    this._result = EnumBetResult.Fail;
                    if (recmV != EnumBjlResult.U) {
                        recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
                    }
                }
            }
            else {
                this._result = EnumBetResult.NotBet;
            }

        }

        return recmV;
    }

}